home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / basics / qtcreatemovie / qtvideo.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.8 KB  |  228 lines

  1. /*
  2.     File:        QTVideo.c
  3.     
  4.     Contains:    Code to create video tracks for QuickTime CreateMovie sample
  5.     
  6.     Written by:    Scott Kuechle
  7.                 (based heavily on QuickTime sample code in Inside Macintosh: QuickTime)
  8.  
  9.     Copyright:    © 1998 by Apple Computer, Inc. All rights reserved
  10.     
  11.     Change History (most recent first)
  12.     
  13.         <1>        6/26/98        srk        first file
  14.  
  15.  
  16. */
  17.  
  18. /************************************************************
  19. *                                                           *
  20. *    INCLUDE FILES                                          *
  21. *                                                           *
  22. *************************************************************/
  23.  
  24.  
  25. #include "ConditionalMacros.h"
  26.  
  27. #if TARGET_OS_WIN32
  28.     #include <QTML.h>
  29. #endif
  30.  
  31. #include <MacTypes.h>
  32. #include <Errors.h>
  33. #include <Fonts.h>
  34. #include <QuickDraw.h>
  35. #include <FixMath.h>
  36. #include <Sound.h>
  37. #include <Movies.h>
  38. #include <ImageCompression.h>
  39. #include <NumberFormatting.h>
  40.  
  41. #include "CreateMovie.h"
  42. #include "QTVideo.h"
  43.  
  44. /************************************************************
  45. *                                                           *
  46. *    FUNCTION PROTOTYPES                                    *
  47. *                                                           *
  48. *************************************************************/
  49.  
  50. static void QTVideo_AddVideoSamplesToMedia (Media theMedia, const Rect *trackFrame);
  51. static void QTVideo_DrawFrame (const Rect *trackFrame, long curSample);
  52.  
  53. /************************************************************
  54. *                                                           *
  55. *    CONSTANTS                                              *
  56. *                                                           *
  57. *************************************************************/
  58.  
  59. #define        kVideoTimeScale     600
  60. #define        kNumVideoFrames     70
  61. #define        kPixelDepth         8    /* use 8-bit depth */
  62. #define        kNoOffset             0
  63. #define        kMgrChoose            0
  64. #define        kSyncSample         0
  65. #define        kAddOneVideoSample    1
  66. #define        kSampleDuration     60    /* frame duration = 1/10 sec */
  67. #define        kTrackStart            0
  68. #define        kMediaStart            0
  69.  
  70.  
  71. /************************************************************
  72. *                                                           *
  73. *    QTVideo_CreateMyVideoTrack()                           *
  74. *                                                           *
  75. *    Creates a video track for a given QuickTime movie      *
  76. *                                                           *
  77. *************************************************************/
  78.  
  79. void QTVideo_CreateMyVideoTrack(Movie theMovie)
  80. {
  81.     Track theTrack;
  82.     Media theMedia;
  83.     OSErr err = noErr;
  84.     Rect trackFrame = {0,0,100,320};
  85.  
  86.         theTrack = NewMovieTrack (theMovie, 
  87.                                 FixRatio(trackFrame.right,1),
  88.                                 FixRatio(trackFrame.bottom,1), 
  89.                                 kNoVolume);
  90.         CheckError( GetMoviesError(), "NewMovieTrack error" );
  91.  
  92.         theMedia = NewTrackMedia (theTrack, VideoMediaType,
  93.                                 kVideoTimeScale, /* Video Time Scale */
  94.                                 nil, 0);
  95.         CheckError( GetMoviesError(), "NewTrackMedia error" );
  96.  
  97.         err = BeginMediaEdits (theMedia);
  98.         CheckError( err, "BeginMediaEdits error" );
  99.  
  100.         QTVideo_AddVideoSamplesToMedia (theMedia, &trackFrame);
  101.  
  102.         err = EndMediaEdits (theMedia);
  103.         CheckError( err, "EndMediaEdits error" );
  104.  
  105.         err = InsertMediaIntoTrack (theTrack, kTrackStart,/* track start time */
  106.                                     kMediaStart, /* media start time */
  107.                                     GetMediaDuration (theMedia),
  108.                                     fixed1);
  109.         CheckError( err, "InsertMediaIntoTrack error" );
  110.  
  111. /************************************************************
  112. *                                                           *
  113. *    QTVideo_AddVideoSamplesToMedia()                       *
  114. *                                                           *
  115. *    Creates video samples for the media in a track         *
  116. *                                                           *
  117. *************************************************************/
  118.  
  119. static void QTVideo_AddVideoSamplesToMedia (Media theMedia, const Rect *trackFrame)
  120. {
  121.     long maxCompressedSize;
  122.     GWorldPtr theGWorld = nil;
  123.     long curSample;
  124.     Handle compressedData = nil;
  125.     Ptr compressedDataPtr;
  126.     ImageDescriptionHandle imageDesc = nil;
  127.     CGrafPtr oldPort;
  128.     GDHandle oldGDeviceH;
  129.     OSErr err = noErr;
  130.  
  131.  
  132.  
  133.         err = NewGWorld (&theGWorld, 
  134.                         kPixelDepth,    /* pixel depth */
  135.                         trackFrame, 
  136.                         nil, 
  137.                         nil, 
  138.                         (GWorldFlags) 0 );
  139.         CheckError (err, "NewGWorld error");
  140.  
  141.         LockPixels (theGWorld->portPixMap);
  142.         err = GetMaxCompressionSize(theGWorld->portPixMap,
  143.                                     trackFrame, 
  144.                                     kMgrChoose, /* let ICM choose depth */
  145.                                     codecNormalQuality, 
  146.                                     kAnimationCodecType, 
  147.                                     (CompressorComponent) anyCodec,
  148.                                     &maxCompressedSize);
  149.         CheckError (err, "GetMaxCompressionSize error" );
  150.  
  151.         compressedData = NewHandle(maxCompressedSize);
  152.         CheckError( MemError(), "NewHandle error" );
  153.  
  154.         MoveHHi( compressedData );
  155.         HLock( compressedData );
  156.         compressedDataPtr = StripAddress( *compressedData );
  157.  
  158.         imageDesc = (ImageDescriptionHandle)NewHandle(4);
  159.         CheckError( MemError(), "NewHandle error" );
  160.  
  161.         GetGWorld (&oldPort, &oldGDeviceH);
  162.         SetGWorld (theGWorld, nil);
  163.  
  164.         for (curSample = 1; curSample <= kNumVideoFrames; curSample++) 
  165.         {
  166.             EraseRect (trackFrame);
  167.  
  168.             QTVideo_DrawFrame(trackFrame, curSample);
  169.  
  170.             err = CompressImage (theGWorld->portPixMap, 
  171.                                 trackFrame, 
  172.                                 codecNormalQuality,
  173.                                 kAnimationCodecType,
  174.                                 imageDesc, 
  175.                                 compressedDataPtr );
  176.             CheckError( err, "CompressImage error" );
  177.  
  178.             err = AddMediaSample(theMedia, 
  179.                                 compressedData,
  180.                                 kNoOffset,    /* no offset in data */
  181.                                 (**imageDesc).dataSize, 
  182.                                 kSampleDuration,    /* frame duration = 1/10 sec */
  183.                                 (SampleDescriptionHandle)imageDesc, 
  184.                                 kAddOneVideoSample,    /* one sample */
  185.                                 kSyncSample,    /* self-contained samples */
  186.                                 nil);
  187.             CheckError( err, "AddMediaSample error" );
  188.         }
  189.         SetGWorld (oldPort, oldGDeviceH);
  190.  
  191.         if (imageDesc)
  192.         {
  193.             DisposeHandle ((Handle)imageDesc);
  194.         }
  195.         if (compressedData)
  196.         {
  197.             DisposeHandle (compressedData);
  198.         }
  199.         if (theGWorld)
  200.         {
  201.             DisposeGWorld (theGWorld);
  202.         }
  203.  
  204.  
  205. /************************************************************
  206. *                                                           *
  207. *    QTVideo_DrawFrame()                                    *
  208. *                                                           *
  209. *    contains code to "draw" a video frame                  *
  210. *                                                           *
  211. *************************************************************/
  212.  
  213. static void QTVideo_DrawFrame (const Rect *trackFrame, long curSample)
  214. {
  215.     Str255 numStr;
  216.  
  217.         ForeColor( redColor );
  218.         PaintRect( trackFrame );
  219.  
  220.         ForeColor( blueColor );
  221.         NumToString (curSample, numStr);
  222.         MoveTo ( (short)(trackFrame->right / 2), (short)(trackFrame->bottom / 2));
  223.         TextSize ( (short)(trackFrame->bottom / 3));
  224.         DrawString (numStr);
  225.